// pages/my/my.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myInfo:'',
    imgs: [],//本地图片地址数组
    picPaths: [],//网络路径
    addr: '请选择位置',
    distance: 100
  },
  
  getStuInfo(account){
    // console.log("account" + account)
    let that = this;
    wx.request({
      url:"https://nottoday.club/index.php/api/Student/getStudentByAccount",
      method: 'POST',
      header: { 'cookie': wx.getStorageSync("sessionid") },
      data:{
        account:account
      },
      success(res){
        // console.log(res)
        if(res.data.status===1){
          let authorId = res.data.data[0].id
          app.globalData.UserInfo.authorId = authorId
          let curName = res.data.data[0].name
          app.globalData.UserInfo.curName = curName
          that.setData({
            myInfo:res.data.data[0]
          })
        }
      }
    })
  },
  getParentInfo(account){
    let that = this;
    wx.request({
      url:"https://nottoday.club/index.php/api/Stuparent/getStuParentByAccount",
      method: 'POST',
      header: { 'cookie': wx.getStorageSync("sessionid") },
      data:{
        account:account
      },
      success(res){
        if(res.data.status===1){
          // console.log(res.data.data)
          let authorId = res.data.data[0].id
          app.globalData.UserInfo.authorId = authorId
          let curName = res.data.data[0].name
          app.globalData.UserInfo.curName = curName
          let childNum = res.data.data[0].childNum
          app.globalData.UserInfo.childNum = childNum
          that.setData({
            myInfo:res.data.data[0]
          })
        }
      }
    })
  },

  getHeadteaInfo(account){
    let that = this;
    wx.request({
      url: "https://nottoday.club/index.php/api/Classmaster/getClassmasterByAccount",
      method: 'POST',
      header: { 'cookie': wx.getStorageSync("sessionid")},
      data:{
        account:account
      },
      header: {
        'content-type': 'application/json', // 默认值
        'cookie': wx.getStorageSync("sessionid")
        //读取sessionid,当作cookie传入后台将PHPSESSID做session_id使用
      },
      success(res) {
        // console.log(res)
        if(res.data.status===1){
          let authorId = res.data.data[0].id
          app.globalData.UserInfo.authorId = authorId
          let curName = res.data.data[0].name
          app.globalData.UserInfo.curName = curName
          let curGrade = res.data.data[0].grade
          app.globalData.UserInfo.curGrade = curGrade
          let curClass = res.data.data[0].class
          app.globalData.UserInfo.curClass = curClass
          that.setData({
            myInfo: res.data.data[0]
          })
        }
      }
    })

  },
  getGradeteaInfo(account){
    let that = this;
    wx.request({
      url:"https://nottoday.club/index.php/api/Grademaster/getGrademasterByAccount",
      method: 'POST',
      header: { 'cookie': wx.getStorageSync("sessionid") },
      data:{
        account:account
      },
      success(res){
        // console.log(res)
        if(res.data.status===1){
          let authorId = res.data.data[0].id
          app.globalData.UserInfo.authorId = authorId
          let curName = res.data.data[0].name
          app.globalData.UserInfo.curName = curName
          let curGrade = res.data.data[0].grade
          app.globalData.UserInfo.curGrade = curGrade
          that.setData({
            myInfo:res.data.data[0]
          })
        }
      }
    })
  },

  bindFormSubmit:function(e){
    var sha1 = require('../../utils/sha1')
    var md5 = require('../../utils/md5')
    var oldpsw = md5.hex_md5(sha1.hex_sha1(e.detail.value.oldpassword))
    var newpsw = md5.hex_md5(sha1.hex_sha1(e.detail.value.newpassword))
    console.log(oldpsw)
    wx.request({
      url:"https://nottoday.club/index.php/api/Student/updateStuPasswd",
      method:'POST',
      data:{
        'account':e.detail.value.account,
        'oldpassword':oldpsw,
        'newpassword':newpsw
      },
      success:function(res){
        console.log(res)
        if(res.data.status === 1){
          console.log("修改密码成功")
        }else{
          console.log("操作失败")
        }
      }
    })
  },
  // 上传课表
  // chooseImageTap: function () {
  //   var that = this;
  //   wx.showActionSheet({
  //     itemList: ['从相册中选择', '拍照'],
  //     itemColor: "#00000",
  //     success: function (res) {
  //       if (!res.cancel) {
  //         if (res.tapIndex == 0) {
  //           that.chooseWxImage('album')
  //         } else if (res.tapIndex == 1) {
  //           that.chooseWxImage('camera')
  //         }
  //       }
  //     }
  //   })
  // },
  // // 图片本地路径
  // chooseWxImage: function (type) {
  //   var that = this;
  //   var imgsPaths = that.data.imgs;
  //   wx.chooseImage({
  //     sizeType: ['original', 'compressed'],
  //     sourceType: [type],
  //     success: function (res) {
  //       console.log(res.tempFilePaths[0]);
  //       that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
  //     }
  //   })
  // },
  // clearStor: function(){
  //   app.globalData.UserInfo = {
  //     username: '',
  //     nowtext: '',
  //     authorId: '',
  //     childNum: '',
  //     curName: '',
  //     curGrade: '',
  //     curClass: ''
  //   }
  // },
  // //上传服务器
  // upImgs: function (imgurl, index) {
  //   var that = this;
  //   wx.uploadFile({
  //     url: 'https://nottoday.club/index.php/api/Upload/uploadImg',
  //     filePath: imgurl,
  //     name: 'file',
  //     header: {
  //       'content-type': 'multipart/form-data'
  //     },
  //     formData: null,
  //     success: function (res) {
  //       console.log(res) //接口返回网络路径
  //       var data = JSON.parse(res.data)
  //       that.data.picPaths.push(data['msg'])
  //       that.setData({
  //         picPaths: that.data.picPaths
  //       })
  //       console.log(that.data.picPaths)
  //     }
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp();
    var UserInfo = app.globalData.UserInfo
    var username = UserInfo.username
    // console.log(username) // 获取   用户名
    var nowtext = UserInfo.nowtext
    // console.log(nowtext) // 获取   登陆权限 
    if(nowtext >= 901 && nowtext <= 999){
      //查询学生
      this.getStuInfo(username) 
    }else if(nowtext >= 889 && nowtext <= 900){
      //查询家长
      this.getParentInfo(username) 
    }else if(nowtext >= 778 && nowtext <= 888){
      //查询班主任
      this.getHeadteaInfo(username) 
    }else if(nowtext >= 11 && nowtext <= 777){
      //查询辅导员
      this.getGradeteaInfo(username) 
    }else{

    }
    
  },
  showMassage: function(){

    var app = getApp();
    var UserInfo = app.globalData.UserInfo
    console.log(app.globalData.UserInfo)
    var username = UserInfo.username
    // console.log(username) // 获取   用户名
    var nowtext = UserInfo.nowtext
    // console.log(nowtext) // 获取   登陆权限 
    if (nowtext >= 901 && nowtext <= 999) {
      //查询学生
      this.getStuInfo(username)
    } else if (nowtext >= 889 && nowtext <= 900) {
      //查询家长
      this.getParentInfo(username)
    } else if (nowtext >= 778 && nowtext <= 888) {
      //查询班主任
      this.getHeadteaInfo(username)
    } else if (nowtext >= 11 && nowtext <= 777) {
      //查询辅导员
      this.getGradeteaInfo(username)
    } else {
    }
  },
  showMassage: function(){

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //选择获取地理位置
  getAddress: function () {
    var that = this
    console.log(that)
    app.getLocation(that)
    // app.getPermission(that);    //传入that值可以在app.js页面直接设置内容
  }, 

  // 上传课表
  chooseImageTap: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  // 图片本地路径
  chooseWxImage: function (type) {
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
      }
    })
  },
  //上传服务器
  upImgs: function (imgurl, index) {
    var that = this;
    wx.uploadFile({
      url: 'https://nottoday.club/index.php/api/Upload/uploadImg',
      filePath: imgurl,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data'
      },
      formData: null,
      success: function (res) {
        console.log(res) //接口返回网络路径
        var data = JSON.parse(res.data)
        that.data.picPaths.push(data['msg'])
        that.setData({
          picPaths: that.data.picPaths
        })
        console.log(that.data.picPaths)
      }
    })
  }
})